﻿using UnityEngine;
using UnityEngine.UI;

namespace UnknowWorld.CardGame
{
    /// <summary>
    /// 游戏控制器
    /// </summary>
    public class CardGameControl : MonoBehaviour
    {
        void OnEnable()
        {
            turnEndButton.onClick.AddListener(TurnEnd);
        }

        void OnDisable()
        {
            turnEndButton.onClick.RemoveListener(TurnEnd);
        }

        /// <summary>
        /// 玩家卡牌控制
        /// </summary>
        public CharacterCardControl playerControl;
        /// <summary>
        /// 敌方卡牌控制
        /// </summary>
        public CharacterCardControl enemeyControl;
        /// <summary>
        /// 玩家UI
        /// </summary>
        public CharacterUI playerUI;
        /// <summary>
        /// 敌方UI
        /// </summary>
        public CharacterUI enemeyUI;
        /// <summary>
        /// 回合结束按钮
        /// </summary>
        public Button turnEndButton;
        /// <summary>
        ///  玩家信息
        /// </summary>
        public Player Player { get { return _player; } }
        /// <summary>
        /// 敌人信息
        /// </summary>
        public Enemey Enemey { get { return _enemey; } }
        /// <summary>
        /// 玩家信息
        /// </summary>
        private Player _player;
        /// <summary>
        /// 敌人信息
        /// </summary>
        private Enemey _enemey;

        /// <summary>
        /// 游戏开始
        /// </summary>
        public void GameStart(Player player, Enemey enemey)
        {
            _player = player;
            _enemey = enemey;
            InitPlayer();
            InitEnemy();
            _playerTurn = true;
            TurnStart();
        }

        /// <summary>
        /// 游戏结束
        /// </summary>
        public void GameFinish()
        {
            RealaseEnemy();
            RealasePlayer();
        }

        #region 初始化玩家
        /// <summary>
        /// 初始化玩家
        /// </summary>
        void InitPlayer()
        {
            playerControl.InitData(_player);
            _player.OnHurt += PlayerOnHurt;
            _player.OnExpandMagic += PlayerOnExpandMagic;
            _player.OnExpandStama += PlayerOnExpandStama;
            playerControl.WhenDrawCard += PlayerDrawCard;
            playerControl.cardContainer.WhenCardUsed += PlayerCardUsed; 
            InitPlayerUI();
        }

        /// <summary>
        /// 重置玩家
        /// </summary>
        void RealasePlayer()
        {
            playerControl.Clear();
            _player.OnHurt -= PlayerOnHurt;
            _player.OnExpandMagic -= PlayerOnExpandMagic;
            _player.OnExpandStama -= PlayerOnExpandStama;
            playerControl.WhenDrawCard -= PlayerDrawCard;
            playerControl.cardContainer.WhenCardUsed -= PlayerCardUsed; 
        }

        /// <summary>
        /// 初始化玩家UI数据
        /// </summary>
        void InitPlayerUI()
        {
            playerUI.SetHealth(_player.Health);
            playerUI.SetMagic(_player.Magic);
            playerUI.SetStamina(_player.Stamina);
            playerUI.SetCardCount(playerControl.CardCount);
        }

        /// <summary>
        /// 玩家抽卡
        /// </summary>
        /// <param name="cards"></param>
        void PlayerDrawCard(Card[] cards)
        {
            playerUI.SetCardCount(playerControl.CardCount);
        }

        /// <summary>
        /// 玩家受伤
        /// </summary>
        /// <param name="value"></param>
        void PlayerOnHurt(int value)
        {
            playerUI.SetHealth(_player.Health);
        }

        /// <summary>
        /// 玩家魔法值变动时
        /// </summary>
        /// <param name="value"></param>
        void PlayerOnExpandMagic(int value)
        {
            playerUI.SetMagic(_player.Magic);
        }

        /// <summary>
        /// 玩家耐力变动时
        /// </summary>
        /// <param name="value"></param>
        void PlayerOnExpandStama(int value)
        {
            playerUI.SetStamina(_player.Stamina);
        }

        /// <summary>
        /// 玩家卡牌使用时
        /// </summary>
        /// <param name="used"></param>
        /// <param name="cardModel"></param>
        void PlayerCardUsed(bool used, CardModel cardModel)
        {
            if (!used)
            {
                Debug.Log("能量不足!");
            }
        }
        #endregion

        #region 初始化敌人
        /// <summary>
        /// 敌人初始位置
        /// </summary>
        public Transform enemyPoint;
        /// <summary>
        /// 敌人预制对象
        /// </summary>
        public EnemyModel enemyPrefab;
        /// <summary>
        /// 敌人
        /// </summary>
        public EnemyModel EnemeyModel{ get { return _enemyModel; }}
        /// <summary>
        /// 敌人
        /// </summary>
        private EnemyModel _enemyModel;

        /// <summary>
        /// 初始化敌人
        /// </summary>
        void InitEnemy()
        {
            _enemyModel = Instantiate(enemyPrefab, enemyPoint);
            _enemyModel.BindData(_enemey);
            enemeyControl.InitData(_enemey);
            enemeyControl.WhenDrawCard += EnemeyDrawCard;
            enemeyControl.cardContainer.WhenCardUsed += EnemeyCardUsed;
            _enemey.OnHurt += EnemeyHurt;
            _enemey.OnExpandMagic += EnemeyOnExpandMagic;
            _enemey.OnExpandStama += EnemeyOnExpandStama;
            InitEnemyUI();
        }

        /// <summary>
        /// 清空敌方数据
        /// </summary>
        void RealaseEnemy()
        {
            enemeyControl.Clear();
            enemeyControl.WhenDrawCard -= EnemeyDrawCard;
            enemeyControl.cardContainer.WhenCardUsed -= EnemeyCardUsed;
            _enemey.OnHurt -= EnemeyHurt;
            _enemey.OnExpandMagic -= EnemeyOnExpandMagic;
            _enemey.OnExpandStama -= EnemeyOnExpandStama;
            if (_enemyModel)
                Destroy(_enemyModel);
        }

        /// <summary>
        /// 初始化敌人UI数据
        /// </summary>
        void InitEnemyUI()
        {
            enemeyUI.SetHealth(_enemey.Health);
            enemeyUI.SetMagic(_enemey.Magic);
            enemeyUI.SetStamina(_enemey.Stamina);
            enemeyUI.SetCardCount(enemeyControl.CardCount);
        }

        /// <summary>
        /// 敌方抽卡
        /// </summary>
        /// <param name="cards"></param>
        void EnemeyDrawCard(Card[] cards)
        {
            enemeyUI.SetCardCount(enemeyControl.CardCount);
        }

        /// <summary>
        /// 敌方受伤时
        /// </summary>
        /// <param name="value"></param>
        void EnemeyHurt(int value)
        {
            enemeyUI.SetHealth(_enemey.Health);
        }

        /// <summary>
        /// 敌方魔法值变动时
        /// </summary>
        /// <param name="value"></param>
        void EnemeyOnExpandMagic(int value)
        {
            enemeyUI.SetMagic(_enemey.Magic);
        }

        /// <summary>
        /// 敌方耐力变动时
        /// </summary>
        /// <param name="value"></param>
        void EnemeyOnExpandStama(int value)
        {
            enemeyUI.SetStamina(_enemey.Stamina);
        }

        /// <summary>
        /// 敌方卡牌使用时
        /// </summary>
        /// <param name="used"></param>
        /// <param name="cardModel"></param>
        void EnemeyCardUsed(bool used, CardModel cardModel)
        {
            if (!used)
            {
                Debug.Log("能量不足!");
            }
        }
        #endregion
        
        #region 回合流程

        /// <summary>
        /// 玩家回合还是敌方回合
        /// </summary>
        private bool _playerTurn;
        /// <summary>
        ///回合数
        /// </summary>
        private int _turnCount;

        /// <summary>
        /// 回合开始
        /// </summary>
        void TurnStart()
        {
            enemeyControl.FullHandCard();
            if (_playerTurn)
            {//玩家回合
                turnEndButton.interactable = true;
                playerControl.FullHandCard();
                _turnCount++;
                Debug.Log("玩家回合");
            }
            else
            {//敌方回合
                turnEndButton.interactable = false;
                enemeyControl.FullHandCard();
                Debug.Log("敌方回合");
            }
        }

        /// <summary>
        /// 回合结束
        /// </summary>
        void TurnEnd()
        {
            if (_playerTurn)
            {//玩家回合

            }
            else
            {//敌方回合

            }
            _playerTurn = !_playerTurn;
            TurnStart();
        }
        #endregion
    }
}